﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using System.Data.SqlClient;
using BookModel;

namespace DAL
{
   
    //管理员信息表
    public class AdminInfoDAL
    {
        #region 查询所有管理员信息
        public List<AdminInfo> GetAdminInfo()
        {
            List<AdminInfo> LAI = new List<AdminInfo>();
            string sql = "select *from AdminInfo";
            if (SQLHelp.OpenConn())
            {
                SqlDataReader sdr = SQLHelp.ExecReader(sql);
                if (sdr != null)
                {
                    while (sdr.Read())
                    {
                        LAI.Add(new AdminInfo
                        {
                            AID = (int)sdr["AID"],
                            Aname = sdr["Aname"] as string,
                            ASex = sdr["ASex"] as string,
                            Abrith = Convert.ToDateTime(sdr["Abrith"]),
                            Apassword = sdr["Apassword"] as string,
                            APhone = sdr["APhone"] as string,
                            Address = sdr["Address"] as string,
                            AdminlimitsID = (int)sdr["AdminlimitsID"],
                            Remark = sdr["Remark"] as string
                        });
                    }
                }
                SQLHelp.CloseConn();
            }
            return LAI;
        }
        #endregion
        #region 新增管理员
        public string insertAdminInfo(AdminInfo ai)
        {
            string sql = "insert into AdminInfo values(@Aname,@ASex,@Abrith,@Apassword,@APhone,@Address,@AdminlimitsID,@Remark)";
            //创建传参数组
            SqlParameter[] sp=new SqlParameter[8];
            sp[0] = new SqlParameter("@Aname",ai.Aname);
            sp[1] = new SqlParameter("@ASex", ai.ASex);
            sp[2] = new SqlParameter("@Abrith", ai.Abrith);
            sp[3] = new SqlParameter("@Apassword", ai.Apassword);
            sp[4] = new SqlParameter("@APhone", ai.APhone);
            sp[5] = new SqlParameter("@Address", ai.Address);
            sp[6] = new SqlParameter("@AdminlimitsID", ai.AdminlimitsID);
            sp[7] = new SqlParameter("@Remark", ai.Remark);

            int count = SQLHelp.ExecQuery(sql,System.Data.CommandType.Text,sp);
            switch (count)
            {
                case -2:
                    sql= "数据库连接出错，请联系管理员";
                    break;
                case -1:
                    sql= "数据输入出错，请检查输入的数据";
                    break;
                case 0:
                    sql= "无任何添加";
                    break;
                default:
                    sql = "添加成功"+count+"条数据";
                    break;
            }

            return sql;
        }
        #endregion
        #region 修改管理员信息
        public string updateAdminInfo(AdminInfo ai)
        {
            string sql = "update AdminInfo set Aname=@Aname,ASex=@ASex,Abrith=@Abrith,Apassword=@Apassword,APhone=@APhone,Address=@Address,AdminlimitsID=@AdminlimitsID,Remark=@Remark where AID=@AID";
            //创建传参数组
            SqlParameter[] sp = new SqlParameter[9];

            sp[0] = new SqlParameter("@Aname", ai.Aname);
            sp[1] = new SqlParameter("@ASex", ai.ASex);
            sp[2] = new SqlParameter("@Abrith", ai.Abrith);
            sp[3] = new SqlParameter("@Apassword", ai.Apassword);
            sp[4] = new SqlParameter("@APhone", ai.APhone);
            sp[5] = new SqlParameter("@Address", ai.Address);
            sp[6] = new SqlParameter("@AdminlimitsID", ai.AdminlimitsID);
            sp[7] = new SqlParameter("@Remark", ai.Remark);
            sp[8] = new SqlParameter("@AID", ai.AID);

            int count = SQLHelp.ExecQuery(sql, System.Data.CommandType.Text, sp);
            switch (count)
            {
                case -2:
                    sql= "数据库连接出错，请联系管理员";
                    break;
                case -1:
                    sql= "数据输入出错，请检查输入的数据";
                    break;
                case 0:
                    sql= "无任何添加";
                    break;
                default:
                    sql = "修改成功"+count+"条数据";
                    break;
            }

            return sql;
        }
        #endregion
    }	
}
